package com.mt.api.modules.controller;

import com.mt.common.response.WebPageResponse;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.log.annotation.Log;
import com.mt.api.modules.domain.AppProductAddBo;
import com.mt.api.modules.domain.bo.ProductCancelBo;
import com.mt.api.modules.domain.bo.ProductStatPageBo;
import com.mt.api.modules.domain.vo.ProductDetailVo;
import com.mt.api.modules.domain.vo.ProductInfoVo;
import com.mt.api.modules.domain.vo.ProductStatByProductNoVo;
import com.mt.api.modules.service.IProductService;
import com.mt.api.util.ParamsValid;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: plover
 * @Date: 2022-05-06
 * @Description:
 */
@RestController
@RequestMapping("/app/product")
@RequiredArgsConstructor
public class ProductController {


    private final IProductService productService;

    @PostMapping("/add")
    @ApiOperation(value = "", notes = "生产单提交接口", httpMethod = "POST")
    @Log("生产单新增")
    public WebResponse addProduct(@RequestBody AppProductAddBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validList("请添加商品数据", bo.getProductDetails());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return productService.addProduct(bo);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "", notes = "生产单修改接口", httpMethod = "POST")
    @Log("生产单修改")
    public WebResponse editProduct(@RequestBody AppProductAddBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validList("请添加商品数据", bo.getProductDetails());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return productService.editProduct(bo);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "", notes = "采购单详情接口", httpMethod = "POST")
    @Log("采购单详情")
    public WebResponse<ProductInfoVo> productDetail(Integer productId) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("采购单id不能为空", productId);
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return productService.productDetail(productId);
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "", notes = "采购单撤销接口", httpMethod = "POST")
    @Log("采购单撤销接口")
    public WebResponse cancelProduct(@RequestBody ProductCancelBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("采购单id不能为空", bo.getProductId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return productService.cancelProduct(bo.getProductId());
    }

    @GetMapping("/getProductDetail")
    @ApiOperation(value = "", notes = "采购单详情接口", httpMethod = "GET")
    @Log("采购单编辑详情接口")
    public WebResponse<ProductDetailVo> getProductDetail(Integer productId) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("采购单id不能为空", productId);
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return productService.getProductDetail(productId);
    }

    @GetMapping("/statByProductNo")
    @ApiOperation(value = "", notes = "采购记录(按单据分组)", httpMethod = "GET")
    public WebPageResponse<ProductStatByProductNoVo> statByProductNo(ProductStatPageBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("pageNum不能为空", bo.getPageNum())
                .validNotNull("pageSize不能为空", bo.getPageSize());
        if (!valid.isValid()) {
            return WebResponseUtils.errorPageParam(valid.getMsg());
        }
        return productService.statByProductNo(bo);
    }

}